Add new lint: manual_slice_match#17232
Conversation
|
rustbot has assigned @samueltardieu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
3c13460 to
cc357a3
Compare
|
Lintcheck changes for 40edded
This comment will be updated if you push new changes |
cc357a3 to
09edb05
Compare
This comment has been minimized.
This comment has been minimized.
09edb05 to
bc8bc1e
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
The following triggers the lint while it shouldn't:
fn slice_len_chain_impure(s: impl Fn() -> &'static [u32]) {
if s().len() == 0 {
println!("empty");
} else if s().len() > 1 {
println!("{} {}", s()[0], s()[1]);
} else {
println!("one");
}
}You have to check that the expressions are equal and don't have side-effects.
Also, a limit would be nice, as this one will generate some particularly ugly code:
if s.is_empty() {
println!("empty");
} else if s.len() > 100 {
println!("large");
} else {
println!("small");
}|
Reminder, once the PR becomes ready for a review, use |
bc8bc1e to
02c2e93
Compare
This comment has been minimized.
This comment has been minimized.
|
Lintcheck - 2 hits on default set (27 crates), 4 hits on extended set (499 crates), no ICEs. |
|
You don't need to post lintcheck results. It's run automatically every time you push and the comment (#17232 (comment)) is kept updated with the change from master. |
|
TIL, thanks |
This comment has been minimized.
This comment has been minimized.
02c2e93 to
5a208ee
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Add a new pedantic lint that detects `if`/`else if` chains whose conditions only inspect the length/emptiness of one and the same slice or `Vec` (via `.is_empty()` or `.len()` compared against an integer literal) and rewrites them as a `match` on a slice pattern. The lint is conservative: it requires at least two conditions over the same receiver and an explicit final `else`, and rejects chains that mix in value guards. It generates concrete arms (`[]`, `[_]`, `[_, _, ..]`) where the length predicates are expressible as slice patterns, and falls back to a help-only diagnostic for comparisons that cannot be a single pattern set (`<`, `<=`, `!=`). Arrays are not linted, since their length is a compile-time constant. The suggestion is `MaybeIncorrect` (bodies are copied verbatim and matching a `Vec` borrows the receiver across the arms). It is gated on the slice-patterns MSRV (1.42).
40edded to
86359a0
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
☔ The latest upstream changes (possibly #17385) made this pull request unmergeable. Please resolve the merge conflicts. |
Add a new pedantic lint that detects
if/else ifchains whose conditions only inspect the length/emptiness of one and the same slice orVec(via.is_empty()or.len()compared against an integer literal) and rewrites them as amatchon a slice pattern.The lint is conservative: it requires at least two conditions over the same receiver and an explicit final
else, and rejects chains that mix in value guards. It generates concrete arms ([],[_],[_, _, ..]) where the length predicates are expressible as slice patterns, and falls back to a help-only diagnostic for comparisons that cannot be a single pattern set (<,<=,!=). Arrays are not linted, since their length is a compile-time constant. The suggestion isMaybeIncorrect(bodies are copied verbatim and matching aVecborrows the receiver across the arms). It is gated on the slice-patterns MSRV (1.42).changelog: Add new lint: manual_slice_match